<?php
/**
 * User:chendenan
 * Created On 2022/3/11 0011 19:34
 */

namespace app\api\validate;
use think\Validate;

class User extends  Validate{
    protected $rule =[
        'username'=>'require',
        'name' => ['require',"checkName"],
        'phone_number'=>'require',
        'email' =>'email',
        'code' => 'require|number|min:4',
        //'type' => 'require|in:1,2',
        'type' => ["require","in"=>"1,2"], //两种不同的方式而已
        'sex' =>['require','in'=>"0,1,2"],
    ];
    protected $message =[
        'username'=>'用户名必须',
        'phone_number' =>'手机号码必须',
        'email' => '邮箱必须',
        'code.require' => '验证码必须',
        'code.number' => '验证码必须为数字',
        'code.min' =>'验证码长度最少4位',
        'type.require' => '类型必须',
        'type.in'  => '类型数值错误',
        'name.require' => '账号必须',
        'name.checkUser' => '用户名格式不正确',
        'sex.require' => '性别必须',
        'sex.in' => '性别数值错误',
    ];
    protected $scene = [
        'send_code' => ['phone_number'],
        'send_mail' => ['email'],
        'login' =>['name','code','type'],
        'update_user' => ['username','sex'],
    ];
    // 自定义验证规则
    protected function checkName($value){
        return (preg_match("/^1\d{10}$/",$value) || filter_var($value, FILTER_VALIDATE_EMAIL)) ? true : false;
    }
}